//
//  XHTrafficPackageDetailViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHTrafficPackageDetailViewController.h"

#import "XHTrafficStateCell.h"

#import "Services.h"

@interface XHTrafficPackageDetailViewController ()

@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSArray * listApiCallRecored;
@property (strong, nonatomic) NSDictionary * detatilDic;

@end

@implementation XHTrafficPackageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHTrafficStateCell" bundle:nil] forCellReuseIdentifier:XHTrafficStateCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getUserTrafficPackageDetail:self.orderSn];
}

- (void)getUserTrafficPackageDetail:(NSString*)orderSn
{
    [ServiceManager getTrafficePackageDetailWithOrderSn:orderSn success:^(NSDictionary *data) {
        self.detatilDic = data;
        self.listApiCallRecored = data[@"listApiCallRecored"];
        [self.ui_tableView reloadData];
    } failure:^(NSDictionary *data) {
    
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger section = 1;
    
    return section;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=self.listApiCallRecored.count;
    
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [self trafficStateCell:indexPath];
    return cell;
}
- (XHTrafficStateCell*)trafficStateCell:(NSIndexPath*)indexPath
{
    XHTrafficStateCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHTrafficStateCellIdentifier];
    
    NSDictionary * dic = self.listApiCallRecored[indexPath.row];
    NSString * unit = @"";
    NSInteger values = [self.detatilDic[@"productValue"] integerValue];
    if (values>1000)
    {
        unit = @"G";
        values = values/1000;
    }
    else if (values<1000)
    {
        unit = @"M";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.ui_phone.text =[NSString stringWithFormat:@"%@ %@ %@",[dic[@"phone"] substringToIndex:3],
                                                               [dic[@"phone"] substringWithRange:NSMakeRange(3,4)],
                                                               [dic[@"phone"] substringFromIndex:7]];
    cell.ui_time.text = [[NSDate dateWithString:dic[@"callDate"] ]stringDateYear];
    cell.ui_number.text = [NSString stringWithFormat:@"%@%ld%@ 编号:%@",self.detatilDic[@"operatorName"],values,unit,dic[@"sequenceNBR"]];
    if ([dic[@"callbackStatus"]isEqualToString:@"CALL_FAIL"])
    {
        cell.ui_static.text = @"失败";
        cell.ui_static.textColor = [ColorManager colorWithHexString:@"#E45252"];
    }
    else if ([dic[@"callbackStatus"]isEqualToString:@"WAIT_FOR_CALL"])
    {
        cell.ui_static.text = @"等待回调";
        cell.ui_static.textColor = [ColorManager colorWithHexString:@"#E45252"];
    }
    else if ([dic[@"callbackStatus"]isEqualToString:@"CALL_SUCCESS"])
    {
        cell.ui_static.text = @"成功";
        cell.ui_static.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.8];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=60;
    return height;
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
